# Matplotlib 可视化工具学习
import numpy as np
from matplotlib import pyplot as plt
import matplotlib as mpl
# 设置中文
mpl.rcParams['font.sans-serif'] = ['SimHei']   # 中文显示
mpl.rcParams['axes.unicode_minus'] = False   # 正负号显示


# 基础入门
# 直方图
def SIM_hist():
    """
    直方图：plt.hist(data,bins=) 
    [data]:数据
    """
    height = [168,155,182,170,173,161,155,173,176,181,166,172,170]  # 数据
    bins = range(150,191,5)  # 区间
    plt.hist(height,bins=bins)
    plt.show()

# 条形图 
def SIM_bar():
    """
    条形图：plt.bar(X,Y) 多用于同类型对比
    """
    classes = ['class 1','class 2','class 3']
    scores = [70,80,60]
    plt.bar(classes,scores)
    plt.show()

# 折线图
def SIM_plot():
    """
    折线图：plt.plot(X,Y) 体现数据的变化率
    """
    year = range(2005,2020)
    height = [157,160,162,163,167,170,173,175,174,179,182,182,182,182,183]
    plt.plot(year,height)
    plt.show()

# 饼图
def SIM_pie():
    """
    饼图：plt.pie(data,labels=,autopct=) 体现数据各项占比
    data:数据
    labels：各项占比的标签
    autopct：百分百显示格式
    """
    labels = ['房贷','饮食','出行','教育']
    data = [8000,2000,2000,3000]
    plt.pie(data,labels=labels,autopct='%1.1f%%')
    plt.show()

# 散点图
def SIM_scatter():
    """
    散点图：plt.scatter(X,Y)
    """
    data = [[18.9,10.4],[21.3,8.7],[19.5,11.6],[20.5,9.7],[19.9,9.4],[22.3,11],[21.4,10.6],[9,9.4],
    [10.4,9],[9.3,11.3],[11.6,8.5],[11.8,10.4],[10.3,10],[8.7,9.5],[14.3,17.2],[14.1,15.5],[14,16.5],
    [16.5,17.7],[15.1,17.3],[16.4,15],[15.7,18]]
    X = [item[0] for item in data]
    Y = [item[1] for item in data]
    plt.title('散点图')
    plt.xlabel('价格（元）')  # 给横坐标添加标题
    plt.ylabel('销量（件）')  # 给纵坐标添加标题
    plt.text(16,16,'牙膏')
    plt.text(10,12,'纸巾')
    plt.text(20,10,'洗衣液')
    plt.scatter(X,Y)
    plt.show()

# 箱线图
def SIM_boxplot():
    """
    箱线图：plt.boxplot(data)  反应原始数据分布的特征(黄线为中位数)
    """
    data = [77,70,72,89,89,70,90,87,94,63,81,99,94,80,95,67,65,88,60,67,85,88,87,75,82,85,95,62,61,93,30]
    plt.boxplot(data)
    plt.show()

# 极线图
def SIM_polar():
    """
    极线图
    """
    r = [1,2,3,4,5]  # 极径
    theta = [0.0,1.570796,3.1415926,4.71238898,6.2831853]  # 角度
    ax = plt.subplot(111,projection='polar')  # 创建子图 指定极坐标轴
    ax.plot(theta,r)
    plt.show()

# 阶梯图
def SIM_step():
    """
    阶梯图：plt.step(X,Y)  反应变化趋势
    """
    year = range(2005,2020)
    height = [157,160,162,163,167,170,173,175,174,179,182,182,182,182,183]
    plt.step(year,height)
    plt.show()


# 图标参数配置
def ADV_param():
    x = [1,2,3]
    y = [80,85,75]
    name = ['A班','B班','C班']
    plt.bar(x,y)
    plt.title('成绩柱状图')  # 图标加入标题
    plt.xlabel('班级')  # 横坐标名称
    plt.ylabel('成绩')  # 纵坐标名称
    plt.xticks(x,name)  # 名称映射
    for i in range(1,4):
        plt.text(i,y[i-1]+1,y[i-1])  # 添加文字
    plt.show()

# 绘制堆积图
def ADV_duiji():
    """
    堆积图：通过bar的堆积成,反应不同分类以及它们总和的趋势
    """
    ch = [72,80,66,77,92]
    math = [62,92,72,75,88]
    eng = [76,81,73,75,80]
    plt.bar(range(1,6),ch,color='red',label='chinese')  # color 设置颜色,label 设置标签
    plt.bar(range(1,6),math,bottom=ch,color='green',label='math')  # bottom 在语文成绩上
    chmath = [ch[i]+math[i] for i in range(5)]
    plt.bar(range(1,6),eng,bottom=chmath,color='blue',label='english')  # bottom在前两个的总和上
    plt.show()

# 分块图
def ADV_fenkuai():
    """
    分块图：plt.bar(X,Y,width=(宽度),fc=(颜色))  不同数据集进行并列解释
    """
    name_list = ['语文','数学','英语']
    c1 = [81.4,83,87.1]
    c2 = [85.6,87.4,90]
    c3 = [78,81.2,86.1]
    width = 0.4
    x = [1,3,5]
    plt.bar(x,c1,label='class1',fc='r',width=width)
    x=[1.4,3.4,5.4]  # 偏移一个宽度
    plt.bar(x,c2,label='class2',fc='g',width=width)
    x=[1.8,3.8,5.8]
    plt.bar(x,c3,label='class3',fc='b',width=width)
    x=[1.4,3.4,5.4]
    plt.xticks(x,name_list)
    plt.legend()  # 显示label
    plt.title('三班级成绩分块图')
    plt.show()

# 气泡图
def ADV_qipao():
    """
    气泡图：plt.scatter(X,Y,s=Z) 三维度的散点图,s=Z 表示气泡大小
    """
    x=[22,22,23,24,25,25,26,27,28,29,30,30,32,32,32,33,34,34,35,36,37,38,38,39,40,42,43,43,45,45,46,48,48,48,50,52,56,57,60,62]
    y=[176,186,164,177,183,194,180,179,190,170,168,192,173,178,181,186,177,187,180,195,179,186,187,190,182,184,176,178,164,185,181,175,173,172,172,169,168,182,188,174]
    z=[70,220,50,170,210,270,150,150,360,150,150,200,150,170,170,160,180,460,480,480,490,300,300,250,300,250,350,180,100,250,160,170,160,180,150,150,130,180,100,160]
    print(len(x),len(y),len(z))
    plt.scatter(x,y,s=z)
    plt.show()


# 第一次测试使用
def first_test():
    x = [1,2]
    y = [3,4]
    plt.title("中文标题")
    # 修改线条的宽度和样式
    plt.rcParams['lines.linewidth'] = 10  # 宽度
    plt.rcParams['lines.linestyle'] = '--'  # 线条类型（虚线）
    # plt.bar(x,y)  
    plt.plot(x,y)
    plt.show()


if __name__ == "__main__":
    # first_test()
    SIM_hist()